<template>
  <div>
    <el-dialog
      title="设备报修"
      :visible.sync="addEditIsDialog_c"
      :close-on-click-modal="false"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="维修模式：" prop="repairMode">
              <el-select
                v-model="ruleForm.repairMode"
                placeholder="请选择维修模式"
                clearable
              >
                <el-option label="检测维修" value="0"></el-option>
                <el-option label="定期维修" value="1"></el-option>
                <el-option label="事后维修" value="2"></el-option>
                <el-option label="改善性维修" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修费用：" prop="repairMoney">
              <el-input-number
                v-model="ruleForm.repairMoney"
                placeholder="请输入维修费用"
                :precision="2"
                :step="1"
                :max="9999999"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="维修时间：" prop="dateArr">
              <el-date-picker
                v-model="ruleForm.dateArr"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="维修计划：" prop="repairPlan">
              <el-input
                v-model="ruleForm.repairPlan"
                placeholder="请输入维修计划"
                type="textarea"
                :rows="2"
                maxlength="200"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { saveDataDeviceRepair } from "@/api/statistic";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        deviceId: "",
        repairMode: "",
        repairMoney: "",
        dateArr: [],
        startDate: "",
        endDate: "",
        repairPlan: "",
      },
      rules: {
        // 表单规则
        repairMode: [
          { required: true, message: "请选择维修模式", trigger: "change" },
        ],
        repairMoney: [
          { required: true, message: "请输入维修费用", trigger: "blur" },
        ],
        dateArr: [
          { required: true, message: "请选择维修时间", trigger: "change" },
        ],
        repairPlan: [
          { required: true, message: "请输入维修计划", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        this.ruleForm.deviceId = this.addEditId;
      }
    },
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.startDate = this.ruleForm.dateArr[0];
          this.ruleForm.endDate = this.ruleForm.dateArr[1];
          saveDataDeviceRepair(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
